home *** CD-ROM | disk | FTP | other *** search
Text File | 1997-04-16 | 8.3 KB | 291 lines | [TEXT/CWIE] |
- { /-------------------------------------------------------------------------------------- }
- { SWGameUtils.c }
- { }
- { Portions are copyright: © 1991-94 Tony Myles, All rights reserved worldwide. }
- { }
- { Description: some utility functions for games }
- { /-------------------------------------------------------------------------------------- }
-
- unit SWGameUtils;
-
- interface
-
- uses
- {$IFC undefined THINK_Pascal}
- Types, Quickdraw,
- {$ENDC}
- QDOffscreen, SWCommonHeaders;
-
-
- procedure InitGameUtils;
-
- procedure Randomize;
- function GetRandom (min: integer; max: integer): integer;
- procedure CenterRect (var rectA: Rect; var rectB: Rect);
- procedure AllowKeyUpEvents;
- procedure RestoreEventMask;
- function HideMenuBar (grafPort: GrafPtr): RgnHandle;
- procedure ShowMenuBar (grafPort: GrafPtr);
- function GetDepthFromWindow (theWindow: WindowPtr): integer;
- function GetScreenDepth (theGDH: GDHandle): integer;
-
- implementation
-
- {$IFC undefined THINK_Pascal}
- uses
- OSUtils, Menus, LowMem, Events, GestaltEqu;
- {$ENDC}
-
- { /-------------------------------------------------------------------------------------- }
- { Randomize - initialize random number seed }
- { /-------------------------------------------------------------------------------------- }
-
- procedure Randomize;
- begin
- {$IFC undefined THINK_Pascal}
- GetDateTime(UInt32(qd.randSeed));
- {$ELSEC}
- GetDateTime(randSeed);
- {$ENDC}
- end;
-
-
- { /-------------------------------------------------------------------------------------- }
- { GetRandom - generate a random number between min and max inclusive }
- { /-------------------------------------------------------------------------------------- }
-
- function GetRandom (min: integer; max: integer): integer;
- var
- value: integer;
- range, temp: longint;
- begin
- value := Random;
- range := (max - min) + 1;
- temp := (value * range) div 65536;
- value := temp + min;
-
- GetRandom := value;
- end;
-
-
- { /-------------------------------------------------------------------------------------- }
- { CenterRect - centers rectA in rectB }
- { /-------------------------------------------------------------------------------------- }
-
- procedure CenterRect (var rectA: Rect; var rectB: Rect);
- var
- width, height: integer;
-
- begin
- width := (rectA.right - rectA.left);
- height := (rectA.bottom - rectA.top);
-
- rectA.left := rectB.left + (((rectB.right - rectB.left) div 2) - (width div 2));
- rectA.top := rectB.top + (((rectB.bottom - rectB.top) div 2) - (height div 2));
- rectA.right := rectA.left + width;
- rectA.bottom := rectA.top + height;
- end;
-
- var
-
- gOldEventMask: integer; { Used by AllowKeyUpEvents and RestoreEventMask }
- eventMaskIsGood: Boolean;
-
- { /-------------------------------------------------------------------------------------- }
- { AllowKeyUpEvents - allows keyUpEvents. Make sure to call RestoreEventMask }
- { before your program quits if you call AllowKeyUpEvents. }
- { /-------------------------------------------------------------------------------------- }
-
- procedure AllowKeyUpEvents;
- begin
- gOldEventMask := LMGetSysEvtMask;
- SetEventMask(everyEvent);
- { Let RestoreEventMask know that the old mask has been saved }
- eventMaskIsGood := true;
- end;
-
-
- { /-------------------------------------------------------------------------------------- }
- { RestoreEventMask - call this before your program quits if you previously }
- { called AllowKeyUpEvents. This will beep if you never called AllowKeyUpEvents first. }
- { /-------------------------------------------------------------------------------------- }
-
- procedure RestoreEventMask;
- begin
- if (eventMaskIsGood) then
- SetEventMask(gOldEventMask)
- else
- SysBeep(1);
- end;
-
-
-
- { /-------------------------------------------------------------------------------------- }
- { Globals for HideMenuBar and ShowMenuBar }
- { /-------------------------------------------------------------------------------------- }
-
- var
-
- gOldVisRgn: RgnHandle; { visRgn of window before hiding menu bar }
- gUpdateRgn: RgnHandle; { region returned to user }
- gOldMBarHeight: integer;
-
-
- { /-------------------------------------------------------------------------------------- }
- { HideMenuBar - expands the vis region of grafPort to cover the entire window, which }
- { will allow you to draw in the top of that window to erase the menu bar. This is a }
- { simple routine designed for programs with only one window that covers the menu bar. }
- { If you need to expand the region of more than one window, you need a different routine. }
- { Be sure to make the window visible before calling this. HideMenuBar returns the }
- { region of the menu bar and corners of the screen, in case you want to erase or }
- { draw in that area. }
- { /-------------------------------------------------------------------------------------- }
-
- procedure InitGameUtils;
- begin
- gOldVisRgn := nil;
- gUpdateRgn := nil;
- eventMaskIsGood := False;
- end;
-
- function HideMenuBar (grafPort: GrafPtr): RgnHandle;
- var
- newVisRgn: RgnHandle;
- savePort: GrafPtr;
- begin
- if (gOldVisRgn <> nil) then
- begin
- HideMenuBar := nil;
- exit(HideMenuBar);
- end;
-
- GetPort(savePort);
- SetPort(grafPort);
-
- gOldMBarHeight := LMGetMBarHeight;
- LMSetMBarHeight(0); { Keeps things like SuperClock from coming on. }
-
- { save off vis region }
- gOldVisRgn := NewRgn;
- CopyRgn(grafPort^.visRgn, gOldVisRgn);
-
- { expand the vis region of the port rect to be completely rectangular }
- newVisRgn := NewRgn;
- RectRgn(newVisRgn, grafPort^.portRect);
- CopyRgn(newVisRgn, grafPort^.visRgn);
- DisposeRgn(newVisRgn);
-
- { set gUpdateRgn to region of rounder corners and menu bar }
- gUpdateRgn := NewRgn;
- CopyRgn(gOldVisRgn, gUpdateRgn);
- DiffRgn(grafPort^.visRgn, gUpdateRgn, gUpdateRgn);
-
- SetPort(savePort);
- HideMenuBar := gUpdateRgn;
- end;
-
- { /-------------------------------------------------------------------------------------- }
- { ShowMenuBar - restores the grafPort to the way it was before the call to HideMenuBar. }
- { Make sure to call this after every call to HideMenuBar to dispose of gOldVisRgn. }
- { /-------------------------------------------------------------------------------------- }
-
- procedure ShowMenuBar (grafPort: GrafPtr);
- var
- savePort: GrafPtr;
- junkRgn: RgnHandle;
-
- begin
-
- if (gOldVisRgn = nil) then
- exit(ShowMenuBar);
-
- GetPort(savePort);
- SetPort(grafPort);
-
- LMSetMBarHeight(gOldMBarHeight);
-
- { fill the rounded corners of the screen with black again }
- junkRgn := NewRgn;
- CopyRgn(gOldVisRgn, junkRgn);
- DiffRgn(grafPort^.visRgn, junkRgn, junkRgn);
-
- {$IFC undefined THINK_Pascal}
- FillRgn(junkRgn, qd.black);
- {$ELSEC}
- FillRgn(junkRgn, black);
- {$ENDC}
-
- DisposeRgn(junkRgn);
-
- { restore the old vis region }
- CopyRgn(gOldVisRgn, grafPort^.visRgn);
- DisposeRgn(gOldVisRgn);
- gOldVisRgn := nil;
-
- DisposeRgn(gUpdateRgn);
- gUpdateRgn := nil;
-
- DrawMenuBar;
-
- SetPort(savePort);
- end;
-
-
- { /-------------------------------------------------------------------------------------- }
- { GetDepthFromWindow }
- { /-------------------------------------------------------------------------------------- }
-
- function GetDepthFromWindow (theWindow: WindowPtr): integer;
- var
- oldGWorld, windowGWld: GWorldPtr;
- oldGDH, windowGDH: GDHandle;
- depth: integer;
-
- begin
- GetGWorld(oldGWorld, oldGDH);
- SetPort(theWindow);
- GetGWorld(windowGWld, windowGDH);
-
- depth := GetScreenDepth(windowGDH);
-
- SetGWorld(oldGWorld, oldGDH);
- GetDepthFromWindow := depth;
- end;
-
- { /-------------------------------------------------------------------------------------- }
- { GetScreenDepth }
- { /-------------------------------------------------------------------------------------- }
-
- function GetScreenDepth (theGDH: GDHandle): integer;
- var
- theSERec: SysEnvRec;
- thePixMap: PixMapHandle;
- err: OSErr;
- begin
-
- err := SysEnvirons(2, theSERec);
- if (not theSERec.hasColorQD) then { no colorQD? }
- GetScreenDepth := 1
- else
- begin
- thePixMap := theGDH^^.gdPMap;
- GetScreenDepth := thePixMap^^.pixelSize; { Depth in bits (1,2,4...) }
- end;
- end;
-
-
- { /-------------------------------------------------------------------------------------- }
- { HasSystem7 }
- { /-------------------------------------------------------------------------------------- }
-
- function HasSystem7: Boolean;
- var
- gestaltResponse: longint;
- err: OSErr;
- begin
- err := Gestalt(gestaltSystemVersion, gestaltResponse);
- HasSystem7 := (err = noErr) and (gestaltResponse >= $0700);
- end;
-
- end.
-